package ldh.im.monitor.cell;

import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import ldh.im.dto.business.monitor.MonitorActiveUserInfo;
import ldh.im.fxbase.emoji.ImageCache;
import ldh.im.fxbase.util.IconUtil;
import ldh.rpc.TerminalType;
import org.controlsfx.control.GridCell;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class MonitorUserCell extends GridCell<MonitorActiveUserInfo> {

    private Label userInfoLabel;

    public MonitorUserCell() {
    }

    @Override
    protected void updateItem(MonitorActiveUserInfo item, boolean empty) {
        super.updateItem(item, empty);
        if (item == null || empty) {
            setGraphic(null);
            setText(null);
            return;
        }

        Pane pane = createCellView(item);
        setGraphic(pane);
    }

    private Pane createCellView(MonitorActiveUserInfo monitorActiveUserInfo) {
        VBox vBox = new VBox();
        vBox.getStyleClass().add("grid-user-cell");
        userInfoLabel = new Label(monitorActiveUserInfo.getMonitorUserInfo().getUserId());
        ImageView imageView = new ImageView();
        imageView.setFitWidth(100);
        imageView.setFitHeight(100);
        imageView.setImage(getHeadImage(monitorActiveUserInfo.getMonitorUserInfo().getHeadImg()));
        userInfoLabel.setGraphic(imageView);
        userInfoLabel.setContentDisplay(ContentDisplay.TOP);

        HBox hBox = createTerminalTypePane(monitorActiveUserInfo.getMonitorUserInfo().getTerminalTypeSet());
        vBox.getChildren().addAll(userInfoLabel, hBox);

        return vBox;
    }

    private HBox createTerminalTypePane(Set<TerminalType> terminalTypeSet) {
        HBox hBox = new HBox();
        List<Node> nodes = terminalTypeSet.stream().map(terminalType -> {
            int iconSize = 16;
            Color iconColor = Color.LIGHTBLUE;
            if (terminalType == TerminalType.pc) {
                return IconUtil.createIcon("fa-tv", iconSize, iconColor);
            } else if (terminalType == TerminalType.android) {
                return IconUtil.createIcon("fa-android", iconSize, iconColor);
            } else if (terminalType == TerminalType.ios) {
                return IconUtil.createIcon("fa-apple", iconSize, iconColor);
            } else if (terminalType == TerminalType.hm) {
                return IconUtil.createIcon("fa-mobile", iconSize, iconColor);
            } else if (terminalType == TerminalType.web) {
                return IconUtil.createIcon("di-html5", iconSize, iconColor);
            } else if (terminalType == TerminalType.pad) {
                return IconUtil.createIcon("fa-tablet", iconSize, iconColor);
            } else if (terminalType == TerminalType.ipad) {
                return IconUtil.createIcon("ion-ipad", iconSize, iconColor);
            }
            return IconUtil.createIcon("fa-bell", iconSize, iconColor);
        }).collect(Collectors.toList());
        hBox.getChildren().addAll(nodes);
        hBox.setAlignment(Pos.CENTER);
        return hBox;
    }

    private Image getHeadImage(String url) {
        if (url == null) {
            return new Image(MonitorUserCell.class.getResource("/imgs/head.jpg").toExternalForm());
        }
        if (!url.contains(":")) {
            String path = ImageCache.getInstance().getImagePath(url);
            if (path != null) {
                return ImageCache.getInstance().getImage(path);
            } else {
                return null;
            }
        }
        return new Image(url);
    }
}
